package com.travel.server.manager.impl.bestsign.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.travel.server.manager.impl.bestsign.dto.LinkDto;
import com.travel.server.manager.impl.bestsign.dto.QueryBindingDto;
import com.travel.server.manager.impl.bestsign.vo.BaseVo;
import com.travel.server.manager.impl.bestsign.vo.LinkVo;
import com.travel.server.manager.impl.bestsign.vo.QueryBindingVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/21 16:47
 * @Desc:
 */
@Slf4j
public class ApiTemplateInvoke {

    private final BestSignClient bestSignClient;

    public final static String SUCCESS = "0";

    private final static String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC1dJfnXe216cOBi9yY61iZXDouDgDMitwOM4gCUWPJ9KlXUsbOG0LnEt9jUxsBRMcvkfnFaVn2ZLC7KxieaIdnIIGw1rN2a0WFJIwBuFrH1yaUUfgMGNOUJjhiph0JLOhQroLntUh//WycHZy8Phw6tQ7S/5f5FKVQi6HCMtcHok70HwU72Axr+e56F4pVGAcaMp7YXjdOmpFPXx6j6GF8FYh2unXUYrBGRH/5rHqAo0Ag8A2zkL47aKCvkucfeZiu4tuehR3unrI4tdAm/CpntsJ2BYvmhRgqntBHK3CW6Gme88jjepbSEkhDjROzlK0QMJeqU+s5HUM1BtLFJtPtAgMBAAECggEACM6w9NT1Tcgb6jTMr0t1EHSOil+5oDP5PGM57crfihTrB0cISUa/d5HN7/c/r08UT/XI5tEXQcNfZKZR2pZ+Q/4q7VdufIf2ZuEAPrEhDuQdhkN4Q7YMxvsX74najPB2Ejx2NCLzcurtE6LIUca9/gd9wbYQPVHIOGGep1tqXn9oF4/QeuFsFZK9GTS5TLGCekREW5gH57Eo2DHSiDlKTgCqjir6+yF2mRPWGGiaLKtDIBsOM2qpxI3C8yjgD5DKy3SrZ/vBe9kBeJ6VZzJqNGYmC7A/FQhdTMD9mshZUPQD3g9z6kmLkxBs0Q3nSGN/j/QBkcuGCal5gO0na3BcbQKBgQD+/GGVBCsCNax/K7LL+MZsLDOrKW1jsWdO8X49Ep/CnF1Exuz38dvQiW+hFo2K3gRB5ZkENWWy+HL+Z0rnwSI2jbRttMxN9q9Sdb8dRG1TazZtLIsOvz1oMF9VZ5PXn68To3oKNVIm1TGh7TdhqMS0VXPvpBiRLsWHX28Xf/6tTwKBgQC2LViECiIjUlKxONb7P68bT1Bs+YMs9329ESEf2ohloW6pql4ZMro9nJr+QPt6mUAgUyWZWWaVLuC/54+a5Y5SGDYBYFVN3iSLVIsttjq1R/YJVD0u6BGo92hqEmCfimQqHaci5WjAGD1zRpVoI9vOSAKhR7u6MkyIstqUrG10AwKBgEK10Onlr0LivACBdEO9EFyYq+Pp8L6WWUrkD3z29Gk784Lc8H5l/nZuno/skJd2QnLjGMdrGPJb4eoBKC2976+KH1xcYt863N+cAqYrktayRAkIEFGJYw1xKl/zu1A8bNece39UN+wE9vlAUK7yMpRjjvNxYSQKso8aPrxNNlotAoGAU7FpZN+y5z8+tiRCv5J2Q7mgXTATz2iz31QrP5MJ7obHbDLUoAbqALwdiIkZ/yzAhRktwNGNiyPKJN+g3axwQc7VoLQ8/FT9vPTOK+X3+qhgo9CLey0qT9G5qmFe+mx9r8uHqURzZyy7rmXS5dDzfkUe0DFAUT4iYvqn6H1+mzkCgYBUGred7m5xXs96xaHHVM5Y2mqpLszHndSMqDMMKUKIWSFBuOsGIqHfua/f+ZWHX4IbvveG5sEtYdBy4xReZRrW/yYcHL+/dIkP4DwE9unlwFnpKQ8F7Ynv7sWx+nilI0RuveSqt3M6HwgpQwaE9WCbhMvr6Gai9yASJuDQYwSttQ==";


    public ApiTemplateInvoke() {
        bestSignClient = new BestSignClient(
                "https://api.bestsign.info",
                "l2ZxwSA6OBs6IgsQ6og9f7ixuPMR7hMQ",
                "3seTeUqUyhGlYZfD3CbGVXPDrcu1ieG4",
                privateKey
        );
    }

    public BaseVo<QueryBindingVo> queryAccount(String accountId){
        QueryBindingDto dto = new QueryBindingDto();
        dto.setDevAccountId(accountId);
        log.info("查询上上签账号绑定关系请求 {}", JSONObject.toJSONString(dto));
        String result = executePost("/api/users/binding-existence",  dto);
        log.info("查询上上签账号绑定关系返回 {}", result);
        if(StringUtils.isBlank(result)){
            return null;
        }
        BaseVo<QueryBindingVo> vo = JSON.parseObject(result, new BaseVo<QueryBindingVo>().getClass());
        if(vo.getData() != null){
            vo.setVo(JSON.parseObject(vo.getData().toJSONString(), QueryBindingVo.class));
        }
        return vo;
    }

    public BaseVo<LinkVo> queryDocUrl(String accountId){
        LinkDto dto = new LinkDto();
        dto.setDevAccountId(accountId);
        dto.setTargetPage("doc");
        log.info("查询生成跳转链接返回 {}", JSONObject.toJSONString(dto));
        String result = executePost("/api/users/sso-link", dto);
        log.info("查询生成跳转链接返回 {}", result);
        if(StringUtils.isBlank(result)){
            return null;
        }
        BaseVo<LinkVo> vo = JSON.parseObject(result, new BaseVo<LinkVo>().getClass());
        if(vo.getData() != null){
            vo.setVo(JSON.parseObject(vo.getData().toJSONString(), LinkVo.class));
        }
        return vo;
    }

    private void bind(){
        JSONObject data = new JSONObject();
        data.put("devAccountId", "tracyLi");
        data.put("userType", 1);
        data.put("targetPage", "doc");
        String result = executePost("/api/users/binding-url", data);
        System.out.println(result);
    }

    private String executePost(String url, Object data){
        String result = bestSignClient.executeRequest(url, "post", data);
        if(StringUtils.isBlank(result)){
            log.error("上上签接口返回空数据");
            return null;
        }
        return result;
    }

}
